package com.xsm.ppsecret.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-7.
 */

public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter {

    private OnChildClickListener mOnChildClickListener;
    private OnLongChildClickListener mOnLongChildClickListener;
    private RecyclerView mRecyclerView;

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        mOnChildClickListener = onChildClickListener;
    }

    public void setOnLongChildClickListener(OnLongChildClickListener onLongChildClickListener) {
        mOnLongChildClickListener = onLongChildClickListener;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        mRecyclerView = null;
    }

    void adapterClick(View view) {
        if (mRecyclerView != null && mOnChildClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(view);
            mOnChildClickListener.onChildClick(mRecyclerView, view, position);
        }
    }

    void adapterLongClick(View view) {
        if (mRecyclerView != null && mOnLongChildClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(view);
            mOnLongChildClickListener.onLongChildClick(mRecyclerView, view, position);
        }
    }

    public interface OnChildClickListener {
        void onChildClick(RecyclerView parent, View view, int position);
    }

    public interface OnLongChildClickListener {
        void onLongChildClick(RecyclerView parent, View view, int position);
    }
}
